<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
    <script>


        var arr1 = [
            {
                "addClass": "userDomainIco",
                "children": [
                    {
                        "addClass": "",
                        "children": [
                            {
                                "addClass": "",
                                "children": [
                                    {
                                        "addClass": "",
                                        "children": [],
                                        "id": "2sh5wxryttt19qf6gs5kvzlc_7",
                                        "isFolder": false,
                                        "name": "业务二组",
                                        "open": false,
                                        "parentId": "",
                                        "pid": "2sh5wxryttt19qf6gs5kvzlc_5",
                                        "title": "业务二组",
                                        "type": "2",
                                        "userSize": 3,
                                        "users": [
                                            {
                                                "mtKey": "8"
                                            },
                                            {
                                                "mtKey": "10"
                                            },
                                            {
                                                "mtKey": "11"
                                            }
                                        ]
                                    },
                                    {
                                        "addClass": "",
                                        "children": [],
                                        "id": "2sh5wxryttt19qf6gs5kvzlc_8",
                                        "isFolder": false,
                                        "name": "业务一组",
                                        "open": false,
                                        "parentId": "",
                                        "pid": "2sh5wxryttt19qf6gs5kvzlc_5",
                                        "title": "业务一组",
                                        "type": "2",
                                        "userSize": 3,
                                        "users": [
                                            {
                                                "mtKey": "9"
                                            },
                                            {
                                                "mtKey": "14"
                                            },
                                            {
                                                "mtKey": "16"
                                            }
                                        ]
                                    }
                                ],
                                "id": "2sh5wxryttt19qf6gs5kvzlc_5",
                                "isFolder": true,
                                "name": "苏州开发团队",
                                "open": false,
                                "parentId": "",
                                "pid": "2sh5wxryttt19qf6gs5kvzlc_1",
                                "title": "苏州开发团队",
                                "type": "2",
                                "userSize": 13,
                                "users": [
                                    {
                                        "mtKey": "13"
                                    },
                                    {
                                        "mtKey": "3"
                                    }
                                ]
                            },
                            {
                                "addClass": "",
                                "children": [],
                                "id": "2sh5wxryttt19qf6gs5kvzlc_2",
                                "isFolder": false,
                                "name": "号码组",
                                "open": false,
                                "parentId": "",
                                "pid": "2sh5wxryttt19qf6gs5kvzlc_1",
                                "title": "号码组",
                                "type": "2",
                                "userSize": 2,
                                "users": [
                                    {
                                        "mtKey": "1"
                                    },
                                    {
                                        "mtKey": "2"
                                    }
                                ]
                            }
                        ],
                        "id": "2sh5wxryttt19qf6gs5kvzlc_1",
                        "isFolder": true,
                        "name": "部门分组",
                        "open": false,
                        "parentId": "",
                        "pid": "2sh5wxryttt19qf6gs5kvzlc",
                        "title": "部门分组",
                        "type": "2",
                        "userSize": 15,
                        "users": []
                    },
                    {
                        "addClass": "other",
                        "children": [],
                        "id": "other",
                        "isFolder": true,
                        "name": "其他",
                        "open": false,
                        "parentId": "",
                        "pid": "",
                        "title": "其他",
                        "type": "2",
                        "userSize": 1,
                        "users": [
                            {
                                "mtKey": "4"
                            }
                        ]
                    }
                ],
                "id": "2sh5wxryttt19qf6gs5kvzlc",
                "isFolder": true,
                "name": "科达科技",
                "open": false,
                "parentId": "",
                "pid": "",
                "title": "科达科技",
                "type": "2",
                "userSize": 16,
                "users": []
            }
        ];

        var arr2 = [
            {
                "addClass": "userDomainIco",
                "children": [
                    {
                        "addClass": "",
                        "children": [
                            {
                                "addClass": "",
                                "children": [
                                    {
                                        "addClass": "",
                                        "children": [],
                                        "id": "2sh5wxryttt19qf6gs5kvzlc_7",
                                        "isFolder": false,
                                        "name": "业务二组",
                                        "open": false,
                                        "parentId": "",
                                        "pid": "2sh5wxryttt19qf6gs5kvzlc_5",
                                        "title": "业务二组",
                                        "type": "2",
                                        "userSize": 3,
                                        "users": [
                                            {
                                                "mtKey": "8"
                                            },
                                            {
                                                "mtKey": "10"
                                            },
                                            {
                                                "mtKey": "11"
                                            }, {
                                                "mtKey": "12"
                                            }
                                        ]
                                    },
                                    {
                                        "addClass": "",
                                        "children": [],
                                        "id": "2sh5wxryttt19qf6gs5kvzlc_8",
                                        "isFolder": false,
                                        "name": "业务一组",
                                        "open": false,
                                        "parentId": "",
                                        "pid": "2sh5wxryttt19qf6gs5kvzlc_5",
                                        "title": "业务一组",
                                        "type": "2",
                                        "userSize": 3,
                                        "users": [
                                            {
                                                "mtKey": "9"
                                            },
                                            {
                                                "mtKey": "14"
                                            },
                                            {
                                                "mtKey": "16"
                                            }
                                        ]
                                    }
                                ],
                                "id": "2sh5wxryttt19qf6gs5kvzlc_5",
                                "isFolder": true,
                                "name": "苏州开发团队",
                                "open": false,
                                "parentId": "",
                                "pid": "2sh5wxryttt19qf6gs5kvzlc_1",
                                "title": "苏州开发团队",
                                "type": "2",
                                "userSize": 13,
                                "users": [
                                    {
                                        "mtKey": "13"
                                    },
                                    {
                                        "mtKey": "3"
                                    }
                                ]
                            },
                            {
                                "addClass": "",
                                "children": [],
                                "id": "2sh5wxryttt19qf6gs5kvzlc_2",
                                "isFolder": false,
                                "name": "号码组",
                                "open": false,
                                "parentId": "",
                                "pid": "2sh5wxryttt19qf6gs5kvzlc_1",
                                "title": "号码组",
                                "type": "2",
                                "userSize": 2,
                                "users": [
                                    {
                                        "mtKey": "1"
                                    },
                                    {
                                        "mtKey": "2"
                                    }
                                ]
                            }
                        ],
                        "id": "2sh5wxryttt19qf6gs5kvzlc_1",
                        "isFolder": true,
                        "name": "部门分组",
                        "open": false,
                        "parentId": "",
                        "pid": "2sh5wxryttt19qf6gs5kvzlc",
                        "title": "部门分组",
                        "type": "2",
                        "userSize": 15,
                        "users": []
                    },
                    {
                        "addClass": "other",
                        "children": [],
                        "id": "other",
                        "isFolder": true,
                        "name": "其他",
                        "open": false,
                        "parentId": "",
                        "pid": "",
                        "title": "其他",
                        "type": "2",
                        "userSize": 1,
                        "users": [
                            {
                                "mtKey": "4"
                            }
                        ]
                    }
                ],
                "id": "2sh5wxryttt19qf6gs5kvzlc",
                "isFolder": true,
                "name": "科达科技",
                "open": false,
                "parentId": "",
                "pid": "",
                "title": "科达科技",
                "type": "2",
                "userSize": 16,
                "users": []
            }
        ];



        // 1. 依次向下计算hash  
        // 3. 如果上层hash一致 下次则无需再计算了
        var orgMap1 = {};  // arr
        var orgMap2 = {}; // arr2


        function setHash(arr, mapObj) {
            for (var i = 0; i < arr.length; i++) {
                var item = arr[i];
                hash = getHash(JSON.stringify(item));
                item.hash = hash;

                mapObj["" + item.id.toString()] = {
                    hash: hash,
                    obj: item
                }


                if (item.children && item.children.length > 0) {
                    setHash(item.children,mapObj);
                }
            }
        }


        setHash(arr1, orgMap1);
        console.log(arr1, "=======arr==========]");

        setHash(arr2, orgMap2);
        console.log(arr2, "=======arr==========]");


        function getHash(input) {
            var I64BIT_TABLE =
                'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'.split('');

            var hash = 5381;
            var i = input.length - 1;

            if (typeof input == 'string') {
                for (; i > -1; i--)
                    hash += (hash << 5) + input.charCodeAt(i);
            }
            else {
                for (; i > -1; i--)
                    hash += (hash << 5) + input[i];
            }
            var value = hash & 0x7FFFFFFF;

            var retValue = '';
            do {
                retValue += I64BIT_TABLE[value & 0x3F];
            }
            while (value >>= 6);
            return retValue;
        }

    </script>

</body>

</html>